<?php

namespace App\Service\Rms;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\ErrCode;
use App\Enums\OpenShop\InvestmentPromotion\OpenShopCacheEnum;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Library\Rms\RmsPayApi;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignCostMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignSoftOutfitMappingModel;
use App\Remote\Rms\RmsShopRemote;
use App\Service\BaseService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;

class ShopService extends BaseService
{

    const SHOP_TYPE_0 = 0;
    const SHOP_TYPE_1 = 1;
    const SHOP_TYPE_2 = 2;

    const SHOP_TYPE_MAPPING = [
        self::SHOP_TYPE_0 => '-',
        self::SHOP_TYPE_1 => '加盟店',
        self::SHOP_TYPE_2 => '直营店',
    ];

    /**
     * 根据 ShopCode 获取门店名称
     *
     * @param int $shopCode
     * @return mixed
     */
    public static function getShopNameByCode(int $shopCode)
    {
        $result = RmsShopRemote::getShopFieldsByCodes([$shopCode]);
        return $result['shopList'][0]['name'] ?? '';
    }

    /**
     * 更新店铺名称
     * 更改名店名称时门店简称同时删除 rms会自动生成简称
     * @param string $orderNo
     * @param string $shopName
     * @throws GuzzleException
     */
    public static function updateShopName(string $orderNo, string $shopName)
    {
        self::updateShopField($orderNo, ['name' => $shopName, 'shortName' => '']);
    }

    /**
     * 更新门店开店时间
     *
     * @param string $orderNo
     * @param int $formalUseAt 时间戳
     * @throws GuzzleException
     */
    public static function updateShopFormalUseAt(string $orderNo, int $formalUseAt)
    {
        self::updateShopField($orderNo, ['formalUseAt' => $formalUseAt]);
    }

    /**
     * 更新门店督导
     *
     * @param string $orderNo
     * @param int $councilorId
     * @throws GuzzleException
     */
    public static function updateShopCouncilorId(string $orderNo, int $councilorId)
    {
        self::updateShopField($orderNo, ['councilorId' => $councilorId]);
    }

    /**
     * 更新店铺信息
     *
     * @param string $orderNo
     * @param array $shopField
     * @throws GuzzleException
     */
    private static function updateShopField(string $orderNo, array $shopField)
    {
        $orderInfo = OpenShopSignOrderModel::query()
                                           ->where('orderNo', $orderNo)
                                           ->first();

        RmsShopRemote::updateShopFieldByCode((int)$orderInfo->shopCode, $shopField);
    }

    /**
     * rms门店名称去头尾
     * @param string $name
     * @return string
     */
    public static function trimShopName(string $name): string
    {
        if (mb_substr($name, 0, 5) == '夸父炸串（') {
            $name = mb_substr($name, 5);

            if (mb_substr($name, -1, 1) == '）') {
                $name = mb_substr($name, 0, mb_strlen($name) - 1);
            }
        } elseif (mb_substr($name, 0, 5) == '夸父炸串(') {
            $name = mb_substr($name, 5);

            if (mb_substr($name, -1, 1) == ')') {
                $name = mb_substr($name, 0, mb_strlen($name) - 1);
            }
        }

        return $name;
    }
}
